package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Jim Chen
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public List<User> list(HttpServletRequest request){
        return userService.findAll();
    }

    @RequestMapping(value="/{id}", method = RequestMethod.GET)
    @ResponseBody
    public User detail(@PathVariable Integer id){
        return userService.getById(id);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public User create(@RequestBody User user){
        return userService.create(user);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public User update(@RequestBody User user){
        return userService.update(user);
    }

    @RequestMapping(value="/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public int delete(@PathVariable Integer id){
        return userService.delete(id);
    }
}
